var WechatAPI = require('wechat-api');
var config = require('config');

var menu       = config.get('wx.menu');
var appid      = config.get('wx.appid');
var appsecret  = config.get('wx.appsecret');

var api = new WechatAPI(appid, appsecret);
function createMenu(callback) {
    api.createMenu(menu, function (err, result) {
        callback(err, result);
    });
}

function getMaterials(callback) {
    api.getMaterialCount(function (err, result, res) {
        if (err) {
            console.log(err);
        } else {
            result.forEach(function (type, total_count) {
                var offset = 0;
                type = type.split("_")[0];
                while (offset < total_count) {
                    api.getMaterials(type, offset, 20, function (err, result, res) {
                        if (err) {
                            console.log(err);
                            callback(err, null);
                            return;
                        } else {
                            offset += result.item_count;
                            result.item.forEach(function (item) {
                                callback(null, item);
                            })
                        }
                    })
                }
            })
        }
    })
}
module.exports.createMenu = createMenu;
module.exports.getMaterials = getMaterials;
